package me.zhengjie.tools.domain.vo

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonTypeName
import me.zhengjie.common.utils.JacksonUtil
import me.zhengjie.modules.system.service.dto.UserDTO
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.NotBlank
import org.apache.commons.lang3.builder.ToStringBuilder
import java.sql.Date
import java.sql.Timestamp

/**
 * 交易详情，按需应该存入数据库，这里存入数据库，仅供临时测试
 * @author Kuki Wu
 * @date 2018-12-31
 */
@JsonTypeName("TradeVo")
data class TradeVo (
    // （必填）商品描述
    var body: @NotBlank String? = null,

    // （必填）商品名称
    var subject: @NotBlank String? = null,

    // （必填）商户订单号，应该由后台生成
    @Schema(description = "商户订单号", required = true, hidden=true)
    var outTradeNo: String? = null,

    // （必填）第三方订单号
    @Schema(description = "第三方订单号", required = true, hidden=true)
    var tradeNo: String? = null,

    // （必填）价格
    var totalAmount: @NotBlank String? = null,

    // 订单状态,已支付，未支付，作废
    @Schema(description = "订单状态,已支付，未支付，作废", required = true, hidden=true)
    var state: String? = null,

    // 创建时间，存入数据库时需要
    @Schema(description = "创建时间，存入数据库时需要", required = true, hidden=true)
    var createTime: Timestamp? = null,

    // 作废时间，存入数据库时需要
    @Schema(description = "作废时间，存入数据库时需要", required = true, hidden=true)
    var cancelTime: Date? = null
){
    annotation class Update

    companion object {
        override fun toString(): String {
            val builder = ToStringBuilder(this)
            val fields = this.javaClass.declaredFields
            try {
                for (f in fields) {
                    f.isAccessible = true
                    builder.append(f.name, f[this]).append("\n")
                }
            } catch (e: Exception) {
                builder.append("toString builder encounter an error")
            }
            return builder.toString()
        }

        fun tojson() {
            JacksonUtil.toJSONString(this)
        }

    }

    fun copy(source: TradeVo?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}